package com.around.test.m3u8;

import cn.hutool.core.io.FileUtil;
import com.around.test.m3u8.util.M3U8Util;
import com.around.test.m3u8.vo.M3U8;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutorService;

import static com.around.test.m3u8.M3U8Download.RETRY_TIMES;
import static com.around.test.m3u8.M3U8Download.initThread;

public class testDownLoadByLocal {
    public static void main(String[] args) throws Exception {
        String folderPath="E:\\电子书\\test";
        String m3u8name="index.m3u8";
        String url="https://txvod.jiakao520.cn/4385540bvodtranscq1500007794/364531953270835011890401912/index.m3u8";
        //解析M3U8地址为对象
        M3U8 m3u8 = M3U8Util.parseM3U8Content(folderPath, m3u8name, url, 3);
        byte[] key = FileUtil.readBytes("E:\\电子书\\科目二\\v1 (2)");
        m3u8.setKey(key);
         String tofile = "E:\\电子书\\tp0\\";
        //根据M3U8对象获取时长
        float duration = M3U8Util.getDuration(m3u8);
        System.out.println(Thread.currentThread().getName()+" 时长: " + ((int) duration / 60) + "分" + (int) duration % 60 + "秒");
        ExecutorService executor=initThread(20,"ddd");

        //根据M3U8对象下载视频段
        M3U8Util.download(m3u8, executor,RETRY_TIMES);

        //关闭线程池
        executor.shutdown();
        System.out.println(Thread.currentThread().getName()+" 等待下载中...");
        while (!executor.isTerminated()) {
            Thread.sleep(50000);
            //System.out.println(Thread.currentThread().getName()+" 等待子线程关闭...");
        }

        //合并文件
        M3U8Util.merge(m3u8, tofile,0);

        System.out.println(Thread.currentThread().getName()+" 下载完成，文件在: " + folderPath);
    }
}
